package list;

public class FindMidList {
    public static void main(String[] args) {
        Node head = new Node((int)(Math.random()*10)+1);
        head.next = new Node((int)(Math.random()*10)+1);
        head.next.next = new Node((int)(Math.random()*10)+1);
        head.next.next.next = new Node((int)(Math.random()*10)+1);
        head.next.next.next.next = new Node((int)(Math.random()*10)+1);

        Node n = head;
        while (n != null) {
            System.out.print(n.value+"\t");
            n = n.next;
        }
        System.out.println();
        Node node = findMid(head);
        System.out.println(node.value);
    }

    private static Node findMid(Node head) {
        Node slow = head;
        Node fast = head;
        while (fast != null && fast.next != null) {
            slow = slow.next;
            fast = fast.next.next;    //---------容易出现空指针
        }
        return slow;
    }

    static class Node {
        private int value;
        private Node prev;
        private Node next;

        public Node(int value) {
            this.value = value;
        }
    }
}
